<?php
namespace YmPhp\Admin\Http\Controllers\Setting;
use YmPhp\Admin\Form;
use YmPhp\Admin\Models\SystemNotify;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Http\Controllers\AdminController;

class SystemNotifyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SystemNotify(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('title')->width('50%');
            $grid->column('is_app')->switch();
            $grid->column('is_site')->switch();
            $grid->column('is_email')->switch();
            $grid->column('is_wechat')->switch();
            $grid->column('state')->switch();
            $grid->column('created_at')->sortable()->width(100);
            $grid->showQuickEditButton();
            $grid->fixColumns(2, -2);
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }


    protected function form(){
        return Form::make(new SystemNotify(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->radio('type')
                ->when(1, function (Form $form) {
                $form->switch('is_email');
                $form->text('email_title');
                $form->text('email_content');
            })->when(2, function (Form $form) {
                    $form->switch('is_sms');
                    $form->text('sms_intempid');
                    $form->text('sms_tempid');
                    $form->text('sms_content');
                    $form->text('sms_note');
            })->when(3, function (Form $form) {
                    $form->switch('is_wechat');
                    $form->text('wechat_tempid');
                    $form->text('wechat_serial');
                    $form->text('wechat_content');
            })->when(4, function (Form $form) {
                    $form->switch('is_site');
                    $form->text('site_title');
                    $form->text('site_content');
            })->when(5, function (Form $form) {
                    $form->switch('is_app');
                    $form->text('app_title');
                    $form->text('app_content');
            })->options(array(
                    1 => __('system-notify.fields.is_email'),
                    2 => __('system-notify.fields.is_sms'),
                    3 => __('system-notify.fields.is_wechat'),
                    4 => __('system-notify.fields.is_site'),
                    5 => __('system-notify.fields.is_app'),
                ))
                ->default(1);
            $form->text('state');
        });
    }
}
